package marking.liu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import marking.liu.constant.LoginConstant;
import marking.liu.pojo.dto.LoginDto;
import marking.liu.pojo.entity.SysUser;
import marking.liu.pojo.vo.LoginVo;
import marking.liu.pojo.vo.Result;
import marking.liu.service.UserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 用户web层接口资源
 */
@RestController
@Api(tags = "用户接口资源")

@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名称查询用户
     * @param userName
     * @return
     */
    @GetMapping("/user/{userName}")
    @ApiOperation("根据用户名称获取用户信息")
    public SysUser getUserByUserName(@PathVariable String userName) {
        return userService.findByUserName(userName);
    }

    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名字", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sessionId", value = "会话id", required = true, dataType = "string", paramType = "query")
    })
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(LoginConstant.SUCCESS, loginVo);
    }

    /**
     * 生成验证码
     */
    @GetMapping("/captcha")
    @ApiOperation("获取生成验证码")
    public Result<Map> getCaptchaCode() {
        Map captchaCode = userService.getCaptchaCode();
        return Result.ok(captchaCode);
    }
}
